<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    // protected $guard = 'user';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'api_token'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [       //指定不希望被返回的字段。
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function generateToken()
    {
       $this->api_token = Str::random(60);
       $this->save();

       return $this->api_token;

       // $user = App\User::find(1);

       // // 创建没有作用域的访问令牌...
       // $token = $user->createToken('Token Name')->accessToken;

       // // 创建有作用域的访问令牌...
       // $token = $user->createToken('My Token', ['place-orders'])->accessToken;
    }
}
